/*
The NIST RCS (Real-time Control Systems)
 library is public domain software, however it is preferred
 that the following disclaimers be attached.
 
Software Copywrite/Warranty Disclaimer
 
   This software was developed at the National Institute of Standards and
Technology by employees of the Federal Government in the course of their
official duties. Pursuant to title 17 Section 105 of the United States
Code this software is not subject to copyright protection and is in the
public domain. NIST Real-Time Control System software is an experimental
system. NIST assumes no responsibility whatsoever for its use by other
parties, and makes no guarantees, expressed or implied, about its
quality, reliability, or any other characteristic. We would appreciate
acknowledgement if the software is used. This software can be
redistributed and/or modified freely provided that any derivative works
bear some notice that they are derived from it, and any modified
versions bear some notice that they have been modified. 
 
*/


/*
 * URLLoadInfoJPanel.java
 *
 * Created on September 26, 2007, 10:04 AM
 */

package diagapplet.utils;

import java.awt.Dimension;

/**
 * Swing JPanel used for displaying progress while loading a URL/File.
 * @author  shackle
 */
public class URLLoadInfoJPanel extends javax.swing.JPanel implements URLLoadInfoPanelInterface
{
    static public boolean ignore_repaint_requests = false;
    public boolean use_color = true;
    long last_repaint_time = 0;
    static final long MAX_REPAINT_TIME = 1000;
    static final long MIN_REPAINT_TIME = 500;
    public String URLname = null;
    public int content_length = -1;
    public int bytes_read = 0;
    Dimension prefDim = null;
    static public boolean debug_on=false;
 
    /** Creates new form URLLoadInfoJPanel */
    public URLLoadInfoJPanel()
    {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel1 = new javax.swing.JLabel();

        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void set_bytes_read(int _bytes_read) throws Exception 
    {
        if(_bytes_read < 0)
        {
            throw new Exception("set_bytes_read("+_bytes_read+")");
        }
        this.bytes_read = _bytes_read;
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                jProgressBar1.setValue(bytes_read);
            }
        });
    }
    

    int last_percent_done=0;
    
    public void inc_bytes_read(int _bytes_read_inc) throws Exception
    {
        if(_bytes_read_inc <= 0)
        {
            throw new Exception("inc_bytes_read("+_bytes_read_inc+")");
        }
        this.bytes_read += _bytes_read_inc;
        if(this.content_length > 1 && this.bytes_read > 1)
        {
            int percent_done = (int) ((100*((long)this.bytes_read))/((long) this.content_length));
            if(percent_done/5 != last_percent_done/5)
            {
                System.err.println( percent_done+"% done.");
                last_percent_done = percent_done;
            }
        }
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                jProgressBar1.setValue(bytes_read);
            }
        });
    }

    public int get_bytes_read()
    {
        return this.bytes_read;
    }

    public void set_content_length(int _content_length)
    {
        this.content_length = _content_length;
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                jProgressBar1.setMaximum(content_length);
            }
        });
    }

    public int get_content_length()
    {
        return this.content_length;
    }

    public void set_URLname(String _URLName)
    {
        this.URLname = _URLName;
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                jLabel1.setText(URLname);
            }
        });
        System.err.println("Loading "+this.URLname);
    }

    public String get_URLname()
    {
        return this.URLname;
    }

    public void updateDisplay()
    {
    }

    public void force_repaint(int i)
    {
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar jProgressBar1;
    // End of variables declaration//GEN-END:variables
    
}
